package cn.hsw.manage.role.controller;


import cn.hsw.component.core.dto.PageDto;
import cn.hsw.component.core.dto.ResultDto;
import cn.hsw.component.core.dto.SelectDto;
import cn.hsw.component.core.enums.HttpEnum;
import cn.hsw.component.core.util.BeanUtil;
import cn.hsw.component.core.util.ResultUtil;
import cn.hsw.component.core.util.Validator;
import cn.hsw.component.data.page.PageUtil;
import cn.hsw.manage.role.entity.Role;
import cn.hsw.manage.role.qo.RoleSearchQo;
import cn.hsw.manage.role.service.RoleService;
import cn.hsw.manage.role.vo.RolePermissionVO;
import cn.hsw.manage.role.vo.RoleVO;
import cn.hsw.manage.user.entity.User;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李子木
 * @since 2021-11-03 08:56:11
 */
@RestController
@RequestMapping("/manage/role")
public class RoleController {

    @Autowired
    RoleService service;

    @PostMapping("/add")
    @RequiresPermissions("manage:role:add")
    public ResultDto add(@Validated RoleVO roleVO){

        service.add(roleVO);

        return ResultUtil.success();
    }

    /**
     * 删除
     * @return  -
     */
    @PostMapping("/del/{pid}")
    @RequiresPermissions("manage:role:del")
    public ResultDto del(@PathVariable("pid")Long pid) throws Exception {
        service.delete(pid);

        return ResultUtil.success();
    }

    /**
     * 修改
     * @param roleVO    要修改的内容
     * @return  -
     */
    @PostMapping("/edit/{pid}")
    @RequiresPermissions("manage:role:edit")
    public ResultDto edit(@PathVariable("pid")Long pid,RoleVO roleVO){


        service.edit(pid,roleVO);

        return ResultUtil.success();
    }

    /**
     * 查 分页列表
     * @return ResultDto<PageDto<RoleModel>
     */
    @PostMapping("/list")
    @RequiresPermissions("manage:role:list")
    public ResultDto<PageDto<Role>> list(RoleSearchQo params){
        IPage<Role> page = service.page(PageUtil.getPage(params.getPage(),params.getSize()));
        return ResultUtil.success(page.getRecords(),page.getTotal());
    }

    @PostMapping("/select")
    @RequiresPermissions("manage:role:select")
    public ResultDto<List<SelectDto>> select(){
        List<Role> list = service.list();

        List<SelectDto> select = BeanUtil.copyListProperties(list, SelectDto::new
                ,(roleModel, selectDto) -> selectDto.setValue(roleModel.getPid()));

        return ResultUtil.success(select);
    }

    /**
     * @return success
     */
    @PostMapping("/authorization")
    @RequiresPermissions("manage:role:authorization")
    public ResultDto authorization(@RequestBody RolePermissionVO rolePermissionVO){

        service.authorization(rolePermissionVO);

        return ResultUtil.success();
    }

    /**
     * @param
     * @return success
     */
    @PostMapping("/auth/{pid}")
    @RequiresPermissions("manage:role:have:auth")
    public ResultDto<RolePermissionVO> roleHaveAuth(@PathVariable("pid")Long rolePid){
        Validator.validateNotEmpty(rolePid, HttpEnum.BAD_REQUEST.getMsg());

        return ResultUtil.success(service.auth(rolePid));
    }

    @PostMapping("/info/{pid}")
    @RequiresPermissions("manage:role:info")
    public ResultDto<Role> info(@PathVariable("pid")Long pid){

        Role roleModel = service.getById(pid);

        return ResultUtil.success(roleModel);
    }
}
